// pages/mine/mine.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:"请登录",
    img:'/images/default.png',
    // grade:0
    information:{}
  },
  sign(){
    let that=this
    //判断
    if(this.data.information.days==1){
      wx.showToast({
        title: '签到成功',
        icon: 'success',
        duration: 2000
      })
     
      //减少次数
      wx.cloud.callFunction({
        name: 'reduceDay',
        data:{
         id:getApp().globalData.oppenid
        },
        success: function(res) {
          that.data.information.days=0
  //  console.log(res)
        },
        fail(res){
          console.log(res)
        }
      })
      //增加积分,
      //获取积分，然后修改积分直接上传
      wx.cloud.callFunction({
        name: 'addPoints',
        data:{
         id:getApp().globalData.oppenid,
         points:10
        },
        success: function(res) {
        console.log(res)
        },
        fail(res){
          console.log(res)
        }
      })
    }
else{
  wx.showToast({
    title: '禁止重复签到',
    icon: 'error',
    duration: 2000
  })
}
  },
  toreward(){
    let that=this
    wx.navigateTo({
      url: '/pages/reward/reward?points='+that.data.information.points,
    })
  },
  toRank(){
wx.navigateTo({
  url: '/pages/rank/rank',
})
  },
  getUserName(){
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        // console.log(res)
        this.setData({
          img: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName
        })
      }
    })
  },
  tohistory(){
    wx.navigateTo({
      url: '/pages/history/history',
    })
  },
  toAbout(){
wx.navigateTo({
  url: '/pages/about/about',
})
  },
  onShow: function () {
    // console.log("进来")
    let that=this
    db.collection('user').where({
user_id:getApp().globalData.oppenid
    })
    .get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        that.setData({
          information:res.data[0]
        })
        // console.log(that.data.information)
      }
    })
  },


})